package com.wbtech.cobub;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import com.wbtech.cobub.CellActivity.WSAsyncTask;
import com.wbtech.cobub.model.Locations;
import com.wbtech.cobub.service.LocationService;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Display;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MapsActivity extends Activity{
	
	private String latitude = "";
	private String longitude = "";
	private String mapURL = "";
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        setContentView(R.layout.static_map);

		Intent intent = getIntent();
		latitude = intent.getStringExtra("latitude");
		longitude = intent.getStringExtra("longitude");
		WindowManager manager = getWindowManager();   
		Display display = manager.getDefaultDisplay(); 
		
		mapURL = "http://ditu.google.cn/staticmap?center="+latitude +
		","+longitude +"&zoom=14&size=" + display.getWidth() + "x"+display.getHeight()+"&maptype=mobile&markers="+latitude +
		","+longitude +",blues";
		WSAsyncTask task = new WSAsyncTask();
		task.execute();
	}
	
	 public Bitmap getURLBitmap(String uriPic)
	 {
	    URL imageUrl = null;
	    Bitmap bitmap = null;
	    try
	    {
	      imageUrl = new URL(uriPic);
	    }
	    catch (MalformedURLException e)
	    {
	      e.printStackTrace();
	    }
	    try
	    {
	     
	      HttpURLConnection conn = (HttpURLConnection) imageUrl
	          .openConnection();
	      conn.connect();
	     
	      InputStream is = conn.getInputStream();
	     
	      bitmap = BitmapFactory.decodeStream(is);
	     
	      is.close();
	    }
	    catch (IOException e)
	    {
	      e.printStackTrace();
	    }
	    return bitmap;
	  }
	
	
	 class WSAsyncTask extends AsyncTask
	    {
	    	ProgressDialog dialog = null;

	    	protected Object doInBackground(Object... params)
	    	{
	    		Bitmap bmp = getURLBitmap(mapURL);
				return bmp;
	    	}

	    	@Override
			protected void onPreExecute() {
				super.onPreExecute();
				dialog = new ProgressDialog(MapsActivity.this);
				dialog.setCancelable(false);
				dialog.setTitle("加载地图...");
				dialog.setMessage("地图加载中,请稍候...");
				dialog.show();
			}

			@Override
			protected void onPostExecute(Object result) {
				super.onPostExecute(result);
		        setProgressBarIndeterminateVisibility(false);
				dialog.dismiss();
				Bitmap bmp = (Bitmap)result;
				if(null != bmp)
				{
					ImageView img = (ImageView)findViewById(R.id.ImageViewMap);
					BitmapDrawable drawable = new BitmapDrawable(bmp);
					img.setImageDrawable(drawable);
				}
				else 
				{
					Toast.makeText(getApplicationContext(), "加载地图失败", Toast.LENGTH_SHORT).show();
				}
			}
	    }
	 
	 private Dialog buildMessageDialog(Context context,String message) {
			AlertDialog.Builder builder = new AlertDialog.Builder(context);
			builder.setIcon(R.drawable.icon);
			builder.setTitle("Cobub提示");
			builder.setMessage(message);
			builder.setPositiveButton("确定",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int whichButton) {
							setTitle("Cobub提示");
						}
					});
			return builder.create();

		}
}
